
package system.base;
import system.data.WarehouseCategoryPageData;
import framework.core.*;

/*
	Author    :chengbiao.he
	CreateTime:2008-02-03 14:56:55
*/
public class WarehouseCategorySrv extends IHandler
{

	public WarehouseCategorySrv() 
	{
		super();
	}
	private void deleteWarehouseCategory(DBConnection con,SrvContext context) throws Exception
	{
		String[] ids = context.getParameterValues("ids");
		WarehouseCategory obj = null;
		for(int i=0;i<ids.length;i++)
		{
			obj = WarehouseCategory.fetch(con,ids[i]);
			if(obj!=null) obj.delete(con);
		}
	}
	private void saveWarehouseCategory(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String id = context.getString("Id",null);
		WarehouseCategory obj;
		WarehouseCategoryPageData data = (WarehouseCategoryPageData)PageData.fromForm(context,WarehouseCategoryPageData.class);
		if((id==null)||(id.equals("")))   //add
		{
			obj = WarehouseCategory.fetch();
			data.setCreateUserId(loginContext.getLoginUserId());
            data.setCreateUser(loginContext.getLoginUserName());
            data.setCreateTime(new DateUtil().getFullString());
            data.setWarehouseCode(loginContext.getLoginWarehouseCode());
            
    	}
    	else                     //update
    	{
    		obj = WarehouseCategory.fetch(con,id);
    	}
		data.setLastUpdateUserId(loginContext.getLoginUserId());
        data.setLastUpdateUser(loginContext.getLoginUserName());
        data.setLastUpdateTime(new DateUtil().getFullString());
		
		context.setValue("WarehouseCategoryPageData",data);
		obj.fill(data);
		obj.save(con);
		
	}
	
	private void listWarehouseCategory(DBConnection con,SrvContext context,String path,ILoginContext loginContext) throws Exception
	{
		WarehouseCategorySearchEvent event = (WarehouseCategorySearchEvent)SearchEvent.fromForm(context,WarehouseCategorySearchEvent.class);
	      //仓库条件加入
        if ((event.getWarehouseCode()==null)||(event.getWarehouseCode().equals(""))){
            event.setWarehouseCode(ConstDefine.getWildCard(loginContext.getLoginWarehouseCode())+"%");
        }
		//event.setWarehouseCode(loginContext.getLoginWarehouseCode());
		context.setSessionValue("WarehouseCategorySearchEvent",event);
		context.setNextUrl(path+"WarehouseCategoryList.jsp");
	}
	private void backWarehouseCategory(DBConnection con,SrvContext context,String path) throws Exception
	{
		context.setNextUrl(path+"WarehouseCategoryList.jsp");
	}
	
	private void selectWarehouseCategory(DBConnection con, SrvContext context, String path) throws Exception
    {
        boolean multi = context.getBoolean("multi");
        String wCode = context.getString("wCode", "");
        context.setValue("multi", new Boolean(multi));
        WarehouseCategorySearchEvent event = (WarehouseCategorySearchEvent)SearchEvent.fromForm(context, WarehouseCategorySearchEvent.class);
        //event.setWarehouseCode(wCode);
        context.setSessionValue("WarehouseCategorySearchEvent", event);
        context.setNextUrl(path + "WarehouseCategorySelect.jsp");
    }
	
	//add by chengbiao.he start
    private void listProductManager(DBConnection con,SrvContext context,String path)throws Exception{
        ProductSearchEvent event =(ProductSearchEvent)SearchEvent.fromForm(context, ProductSearchEvent.class);
        context.setSessionValue("ProductSearchEvent",event);
        context.setNextUrl(path+"ProductManagerList.jsp");
    }
  //add by chengbiao.he end
    
	public void doAction(DBConnection con,SrvContext context,ILoginContext loginContext) throws Exception
	{
		String action_code = context.getActionCode();
		String path = "/system/base/";
		
		if(action_code.equalsIgnoreCase("listWarehouseCategory"))
		{
			listWarehouseCategory(con,context,path,loginContext);
			return;
		}
	
		if(action_code.equalsIgnoreCase("viewWarehouseCategory"))
		{
			String id = context.getString("Id",null);
			WarehouseCategory obj = WarehouseCategory.fetch(con,id);
			context.setValue("WarehouseCategory",obj);
			context.setNextUrl(path+"WarehouseCategoryView.jsp");
			return;
		}
		//add by chengbiao.he start
		
		//add by chengbiao.he end
		if(action_code.equalsIgnoreCase("deleteWarehouseCategory"))
		{
			context.setErrorUrl(path+"WarehouseCategoryList.jsp");
			deleteWarehouseCategory(con,context);
			backWarehouseCategory(con,context,path);
			return;
		}
			
		if(action_code.equalsIgnoreCase("toAddWarehouseCategory"))
		{
			WarehouseCategory obj = WarehouseCategory.fetch();
			WarehouseCategoryPageData data = (WarehouseCategoryPageData)PageData.fromTable(obj,WarehouseCategoryPageData.class);
			context.setValue("WarehouseCategoryPageData",data);
			context.setNextUrl(path+"WarehouseCategoryEdit.jsp");
			return;
		}
		
		
		if(action_code.equalsIgnoreCase("toUpdateWarehouseCategory"))
		{
		    context.setErrorUrl(path+"WarehouseCategoryList.jsp");
            String id = context.getString("Id",null);
            Conditions cs = new Conditions();
            cs.add("id", id.split(":")[0]);
            cs.add("WAREHOUSE_CODE", id.split(":")[1]);
            WarehouseCategory obj = WarehouseCategory.fetch(con, cs);
            
            if (obj!=null){
                obj.canUpdateWarehouseCategory(con,loginContext.getLoginWarehouseCode());
            }
            
            WarehouseCategoryPageData data = (WarehouseCategoryPageData)PageData.fromTable(obj,WarehouseCategoryPageData.class);
            context.setValue("WarehouseCategoryPageData",data);
            context.setNextUrl(path+"WarehouseCategoryEdit.jsp");	
			return;
		}
		/*
		if(action_code.equalsIgnoreCase("toCopyWarehouseCategory"))
		{
			String id = context.getString("Id",null);
			WarehouseCategory obj = WarehouseCategory.copy(WarehouseCategory.fetch(con,id));
			WarehouseCategoryPageData data = (WarehouseCategoryPageData)PageData.fromTable(obj,WarehouseCategoryPageData.class);
			context.setValue("WarehouseCategoryPageData",data);
			context.setNextUrl(path+"WarehouseCategoryEdit.jsp");
			return;
		}
		*/
		if(action_code.equalsIgnoreCase("saveWarehouseCategory"))
		{
			context.setErrorUrl(path+"WarehouseCategoryEdit.jsp");
			saveWarehouseCategory(con,context,loginContext);
			backWarehouseCategory(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("saveAndNewWarehouseCategory"))
		{
			context.setErrorUrl(path+"WarehouseCategoryEdit.jsp");
			saveWarehouseCategory(con,context,loginContext);
			WarehouseCategory obj = WarehouseCategory.fetch();
			WarehouseCategoryPageData data = (WarehouseCategoryPageData)PageData.fromTable(obj,WarehouseCategoryPageData.class);
			context.setValue("WarehouseCategoryPageData",data);
			context.setNextUrl(path+"WarehouseCategoryEdit.jsp");			
			return;
		}
		if(action_code.equalsIgnoreCase("backListWarehouseCategory"))
		{
			backWarehouseCategory(con,context,path);
			return;
		}
		if(action_code.equalsIgnoreCase("selectWarehouseCategory")){
		    selectWarehouseCategory(con, context, path);
		    return;
		}
	    if(action_code.equalsIgnoreCase("toCopyWarehouseCategory")){
            context.setErrorUrl(path + "ProductManageList.jsp");
            String warehouseCode = context.getString("s_WarehouseCode", "");
             ProductSearchEvent event = (ProductSearchEvent) SearchEvent.fromForm(context, ProductSearchEvent.class);
            event.setCategoryCode("");
            event.setSearhWarehouseCode(warehouseCode);
            event.setIsWarehouseCategoryProduct("1");
            context.setValue("warehouseCode", warehouseCode);
            context.setSessionValue("ProductSearchEvent", event);
            context.setNextUrl(path + "AddProductList.jsp");
            //context.setSessionValue("warehouseCategoryId",context.getString("warehouseCategoryId", ""));
            return;
	    }
		//add by chengbiao.he start
        if(action_code.equalsIgnoreCase("toProductManage")){
            ProductSearchEvent event = (ProductSearchEvent)SearchEvent.fromForm(context,ProductSearchEvent.class);
            if(!event.getProviderCode().equals("")){
                Conditions cs = new Conditions();
                cs.add(Provider.NAME, event.getProviderCode());
                ObjectList list = Provider.fetch().search(con, cs);
                if (list.size()>0)
                {
                    String[] providerCodes = new String[list.size()];
                    for(int i=0;i<list.size();i++)
                    {
                        Provider pro = (Provider)list.get(i);
                        providerCodes[i] = pro.getCode();
                    }
                    event.setProviderCodes(providerCodes);
                }
            }
            
            context.setSessionValue("ProductSearchEvent",event);
            
            
            context.setNextUrl(path+"ProductManagerList.jsp");
            return;
        }
		//add by chengbiao.he end

		Logger.throwException("WarehouseCategorySrv","unknown action_code="+action_code);
	}
}
